package controller

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
	"apitestAfterGo/service"
	"apitestAfterGo/utils"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/iris/v12/mvc"
)

type SystemModuleController struct {
}

func (s *SystemModuleController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/getAllSystemModuleByPage", "GetAllSystemModuleByPage")
	b.Handle("GET", "/getSystemModuleBySystemId/{id:int}", "GetSystemModuleBySystemId")
	b.Handle("POST", "/getModulesByList", "GetModulesByList")
	b.Handle("POST", "/addModule", "AddModule")
	b.Handle("PUT", "/modifyModuleById", "ModifyModuleById")
	b.Handle("GET", "/getModuleById/{id:int}", "GetModuleById")
	b.Handle("DELETE", "/deleteModuleById/{id:int}", "DeleteModuleById")
}

func (s *SystemModuleController) GetAllSystemModuleByPage(dto dto.SystemModuleInputDto) common.Result {
	modules, count := service.SystemModuleServiceImpl.GetAllSystemModuleByPage(dto)
	pages := utils.GetPages(count, dto.PageSize)
	page := domain.Page{
		Pages:   pages,
		Total:   count,
		Current: dto.Current,
		Size:    dto.PageSize,
		Records: modules,
	}
	return utils.Success(page, "获取当前系统模块成功")
}

func (s *SystemModuleController) GetSystemModuleBySystemId(id int) common.Result {
	modules := service.SystemModuleServiceImpl.GetSystemModuleBySystemId(id)
	return utils.Success(modules, "删除当前模块成功")
}

func (s *SystemModuleController) GetModulesByList(dto dto.SystemModuleInputDtoByList) common.Result {
	modules := service.SystemModuleServiceImpl.GetModulesByList(dto)
	return utils.Success(modules, "获取系统模块成功")
}

func (s *SystemModuleController) AddModule(context *context.Context, domain domain.SystemModule) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	domain.CreateBy = username
	domain.UpdateBy = username
	modules := service.SystemModuleServiceImpl.AddModule(domain)
	return utils.Success(modules, "添加模块成功")
}

func (s *SystemModuleController) ModifyModuleById(context *context.Context, domain domain.SystemModule) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	domain.UpdateBy = username
	modules := service.SystemModuleServiceImpl.ModifyModuleById(domain)
	return utils.Success(modules, "添加模块成功")
}

func (s *SystemModuleController) GetModuleById(id int) common.Result {
	modules := service.SystemModuleServiceImpl.GetModuleById(id)
	return utils.Success(modules, "获取当前模块详情成功")
}

func (s *SystemModuleController) DeleteModuleById(id int) common.Result {
	modules := service.SystemModuleServiceImpl.DeleteModuleById(id)
	return utils.Success(modules, "获取当前模块详情成功")
}
